from django.conf import settings
from django.db import models
import uuid
from django.utils import timezone
from django.contrib.auth.hashers import make_password
from django.contrib.auth.models import UserManager, AbstractBaseUser

from django.utils import timezone


# Create your models here.
# {
# "predicate": "/people/person/place_lived",
# "object": "San Antonio",
# "subject": "Henry B. Gonzalez"}


class GlobalTriples(models.Model):
    # predict
    # mobject
    # subject
    id = models.AutoField(primary_key=True)
    mobject = models.CharField(max_length=200, default="null")  # 默认存储为点类型
    predict = models.CharField(max_length=200, default="null")
    subject = models.CharField(max_length=200, default="null")
    symbolSize = models.IntegerField(default=2, null=False)
    type = models.IntegerField(blank=True, default=0)

    class Meta:
        verbose_name = "NodePairsModel",
        ordering = ['id']


class UsersManager(UserManager):
    pass


class User(AbstractBaseUser):

    def __str__(self):
        return self.username

    class Meta:
        verbose_name = '用户信息'
        verbose_name_plural = '用户信息'

    ROLES = (('admin', "admin"), ('user', "user"))

    id = models.UUIDField(primary_key=True,
                          default=uuid.uuid4, db_index=True, editable=False)
    username = models.CharField('username', max_length=12, )
    contact = models.CharField(max_length=50, null=True)
    tel = models.CharField(max_length=20, blank=True, null=True)
    email = models.EmailField('email', unique=True, blank=False, null=False)
    is_active = models.BooleanField(default=False)
    is_superuser = models.BooleanField(default=False)
    is_staff = models.BooleanField(default=True)
    date_joined = models.DateTimeField(default=timezone.now)
    is_first_time_login = models.BooleanField(default=True)
    password = models.CharField('password', max_length=30)
    password_mtime = models.DateTimeField(null=True)
    role = models.CharField('role', max_length=32, default="user", choices=ROLES)
    objects = UsersManager()

    USERNAME_FIELD = 'username'
    REQUIRED_FIELDS = ['email']

    # def set_password(self, raw_password):
    #     self.password = make_password(raw_password, None, 'pbkdf2_sha256')
    #     self.password_mtime = timezone.now()

    def save(self, *args, **kwargs):
        self.password = make_password(self.password, None, 'pbkdf2_sha256')
        self.password_mtime = timezone.now()
        super(User, self).save(*args, **kwargs)


class AllDOC(models.Model):
    id = models.UUIDField(primary_key=True,
                          default=uuid.uuid4, db_index=True, editable=False)
    file = models.FileField(upload_to="uploads_to_process/%Y%D/")
    fullname = models.CharField(max_length=200, default="empty", null=False)
    filename = models.CharField(max_length=200, default="0")
    upDate = models.DateTimeField(editable=False, default=timezone.now, null=False, verbose_name="up date",
                                  auto_created=True)
    username = models.CharField('username', max_length=12, )
    Processed = models.BooleanField(default=False, blank=False, verbose_name="is_process")

    class Meta:
        verbose_name = "AllDOCModel"
        ordering = ['id']
